<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019\2\23 0023
 * Time: 16:32
 */

namespace app\Lib\Model\LotteryQualification;


use app\Lib\Exception\BlException;
use app\Lib\Exception\SysException;
use app\Lib\Model\Present\Present;

class Lottery implements \app\Lib\InterfaceIn\LotteryQualification\Lottery
{
    private $userMobile;
    private $lottery;
    /** @var Present $lotteryGoods */
    private $lotteryGoods;
    public function __construct($userMobile, $lottery)
    {
        $this->userMobile = $userMobile;
        $this->lottery = $lottery;
    }

    /**
     * 验证代码
     * @throws BlException
     */
    private function check(){
        if(empty($this->lotteryType)){
            throw new BlException('抽奖类型不能为空');
        }
        if(empty($this->userMobile)){
            throw new BlException('用户号码不能为空');
        }
    }

    /**
     * 验证抽奖资格
     * @return bool
     * @throws BlException
     */
    private function checkLotteryNum(){
        //验证抽奖资格
        $model = (new LotteryQualificationOperate());
        $model->setUserMobile($this->userMobile);
        $model->setLotteryType($this->lottery);
        if(!$model->checkUserLotteryNum()){
            throw new BlException('尊敬的用户您暂无抽奖资格');
        };
        return true;
    }

    /**
     * 执行抽奖
     * @return bool
     */
    private function lotteryRun(){
        //获取所有奖品
        $goodsTable = new \app\Lib\Model\DbModel\Present();
        $lotteryGoods = $goodsTable
            ->where(
            [
                $goodsTable->_present_type=>$this->lottery,
                $goodsTable->_present_num=>['gt',0]
            ]
            )->column($goodsTable->_probability_num,$goodsTable->_id);
        //抽奖代码
        static $arr = array();
        $key = md5(serialize($lotteryGoods));
        if (!isset($arr[$key])) {
            $max = array_sum($lotteryGoods);
            foreach ($lotteryGoods as $k=>$v) {
                $v = $v / $max * 10000;
                for ($i=0; $i<$v; $i++) $arr[$key][] = $k;
            }
        }
        $goodsId = $arr[$key][mt_rand(0,count($arr[$key])-1)];
        $this->lotteryGoods = new Present($goodsId);
        return true;
    }

    /**
     * 记录中奖日志
     * @return bool
     * @throws BlException
     */
    private function recordLottery(){
        $model = (new RecordLottery());
        $model->setMobilePhone($this->userMobile);
        $model->setPresent($this->lotteryGoods);
        $model->run();
        return true;
    }

    /**
     * 扣除奖品数量
     * @return bool
     * @throws SysException
     */
    private function reducePresent(){
        $model = new \app\Lib\Model\DbModel\Present();
        $result = $model->where([$model->_id=>$this->lotteryGoods->getPresentId()])->dec($model->_present_num);
        if(!$result){
            throw new SysException('扣除奖品失败');
        }
        return true;
    }

    /**
     * 扣除抽奖资格
     * @return bool
     * @throws BlException
     * @throws SysException
     */
    private function reduceUserLottery(){
        $model = (new LotteryQualificationOperate());
        $model->setUserMobile($this->userMobile);
        $model->setLotteryType($this->lottery);
        $model->decreaseNum();
        return true;
    }

    /**
     * 执行
     * @throws BlException
     * @throws SysException
     */
    public function run()
    {
        //验证参数
        $this->check();
        //验证抽奖资格
        $this->checkLotteryNum();
        //执行抽奖代码
        $this->lotteryRun();
        //记录中奖记录
        $this->recordLottery();
        //扣除抽奖资格
        $this->reduceUserLottery();
        //扣除产品数量
        $this->reducePresent();
        //发短信通知 TODO
    }




}